﻿package live.tag;


import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;

public class PaginationTag extends TagSupport {
	private static final long serialVersionUID = -3060177524542609945L;
	private String pageNo;
	private String pageSize;
	private String totalRows;
	
	
	public String getPageNo() {
		return pageNo;
	}
	public void setPageNo(String pageNo) {
		try {
			this.pageNo = (String) ExpressionEvaluatorManager.evaluate(
					"pageNo", pageNo, Object.class, this,
					this.pageContext);
		} catch (JspException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	public String getPageSize() {
		return pageSize;
	}
	public void setPageSize(String pageSize) {
		try {
			this.pageSize = (String) ExpressionEvaluatorManager.evaluate(
					"pageSize", pageSize, Object.class, this,
					this.pageContext);
		} catch (JspException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public String getTotalRows() {
		return totalRows;
	}
	public void setTotalRows(String totalRows) {
		try {
			this.totalRows = (String) ExpressionEvaluatorManager.evaluate(
					"totalRows", totalRows, Object.class, this,
					this.pageContext);
		} catch (JspException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	
	private int showCount=3;//显示页码数
	public int doEndTag() throws JspException {
		int totalrows = Integer.parseInt(totalRows);
		int pagesize = Integer.parseInt(pageSize);
		int pageno = Integer.parseInt(pageNo);
//		if(totalrows<=pagesize){
//			try {
//				this.pageContext.getOut().write("");
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//			return super.doEndTag();
//		}
		if(pagesize<1)pagesize=10;
		int pageCount=(totalrows+pagesize-1)/pagesize;
		pageno=pageno>pageCount?pageCount:pageno;
		pageno=pageno<1?1:pageno;
		String action;
		StringBuffer sb=new StringBuffer();
		//sb.append("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr>");
		//首页、上一页按钮
		//sb.append("<td>");
		if(pageno>1){
			sb.append("<a href=\"javascript:void(0);\" onclick=\"changePage(1);return false;\" >首页</a>"); 
			sb.append("<a href=\"javascript:void(0);\" onclick=\"changePage("+(pageno-1)+");return false;\" >上一页</a>"); 
		}else{
			sb.append("<span class=\"disabled\">首页</span>");
			sb.append("<span class=\"disabled\">上一页</span>");
		}
		//sb.append("</td>");
		//中间页码
		//sb.append("<td>");
		if(pageCount>showCount){//显示部分页码
			 if(pageno>showCount/2){
				 int after=pageno+(showCount/2);
				 int front=showCount/2;
				 if(showCount%2==0){
					 front--;
				 }
				 front=pageno-front;
				 if(after>pageCount){
					 after=pageCount;
					 front=pageCount-showCount+1;
				 }
				 if(front>1){
					 sb.append("...");
				 }else{
					 front=1;
				 }
				 for(int i=front;i<=after;i++){
					if(i==pageno){
						sb.append("<span class=\"current\">"+i+"</span>");
					}else{
						sb.append("<a href=\"javascript:void(0);\" onclick=\"changePage("+i+");return false;\" >"+i+"</a>");
					}
				 }
				 if(after<pageCount){
					 sb.append("...");
				 }
			 }else{//显示前几页
				 for(int i=1;i<=showCount;i++){
					if(i==pageno){
						sb.append("<span class=\"current\">"+i+"</span>");
					}else{
						sb.append("<a href=\"javascript:void(0);\" onclick=\"changePage("+i+");return false;\" >"+i+"</a>");
					}
				 }
				 sb.append("...");
			 }
		}else{//显示全部页码
			for(int i=1;i<=pageCount;i++){
				if(i==pageno){
					sb.append("<span class=\"current\">"+i+"</span>");
				}else{
					sb.append("<a href=\"javascript:void(0);\" onclick=\"changePage("+i+");return false;\" >"+i+"</a>");
				}
			}
		}
		//sb.append("</td>");
		
		//下一页、末页按钮
		//sb.append("<td>");
		if(pageno<pageCount){
			sb.append("<a href=\"javascript:void(0);\" onclick=\"changePage("+(pageno+1)+");return false;\">下一页</a>"); 
			sb.append("<a href=\"javascript:void(0);\" onclick=\"changePage("+pageCount+");return false;\">末页</a>"); 
		}else{
			sb.append("<span class=\"disabled\">下一页</span>");
			sb.append("<span class=\"disabled\">末页</span>");
		}
		sb.append("</td>");
		sb.append("<td>");
		sb.append("<span class=\"abc\">");
		sb.append("<input id=\"page\" type=\"hidden\" class=\"xminput\" name=\"page\" value='").append(pageno).append("'/>");
		sb.append("到第<input id=\"page_go\" class=\"xminput\" name=\"page_go\" value=''");
		sb.append(" onkeydown=\"valNum(this)\">&nbsp;页");
		sb.append("<input type=\"button\" value=\"确定\" class=\"buttonss\" ");
		sb.append("onclick=\"surePage();return false;\"");
		sb.append(" />");
		sb.append("</span>");
		sb.append("</td>");
		sb.append("</tr></table>");
		sb.append("\n<script type=\"text/javascript\">\n");
		sb.append("function surePage(){\n");
		sb.append("var objVal=document.getElementById('page').value;\n");
		sb.append("var go=document.getElementById('page_go').value;\n");
		sb.append("var strP=/^\\d+?$/;\n");
		sb.append("if(strP.test(go)==false){;\n");
		sb.append("objVal=").append(pageno).append(";}else{\n");
		sb.append("objVal=go;};\n");
		sb.append("changePage(objVal);\n");
		sb.append("}\n");
		sb.append("function changePage(pageNo){\n");
		sb.append("document.getElementById('page').value=pageNo;\n");
		sb.append("document.forms[0].submit();\n");
		sb.append("}\n");
		sb.append("function valNum(obj){\n");
		sb.append("var strP=/^\\d+?$/;");
		sb.append("if(strP.test(obj.value)){obj.value=").append(pageno).append("};\n");
		sb.append("}");
		sb.append("</script>\n");
		try {
			this.pageContext.getOut().write(sb.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return super.doEndTag();
	}
	
}
